import com.webservice.dto.Table;
import com.webservice.service.RmGetDataApiService;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

public class XmlTest2 {
    @Test
    public void test() throws IOException, DocumentException {
        String xmlStr = "<?xml version=\"1.0\" encoding = \"UTF-8\"?><root>" +
                "<head><H>" +
                "<SUP_ID>163</SUP_ID>" +
                "<SUP_NO>500163</SUP_NO>" +
                "<SUP_NAME>深圳市晟安达光电有限公司</SUP_NAME>" +
                "       <DISABLE>N</DISABLE>" +
                "       <ERP_SUP_ID>162</ERP_SUP_ID>" +
                "       <ERP_SUP_NO>材料采购</ERP_SUP_NO>" +
                "       <OU_ID>81</OU_ID>" +
                "      </H><H>" +
                "       <SUP_ID>164</SUP_ID>" +
                "       <SUP_NO>500164</SUP_NO>" +
                "       <SUP_NAME>深圳市骐通胶粘制品有限公司</SUP_NAME>" +
                "       <DISABLE>N</DISABLE>" +
                "       <ERP_SUP_ID>163</ERP_SUP_ID>" +
                "       <ERP_SUP_NO>材料采购</ERP_SUP_NO>" +
                "       <OU_ID>81</OU_ID>" +
                "      </H><H>" +
                "       <SUP_ID>165</SUP_ID>" +
                "       <SUP_NO>500165</SUP_NO>" +
                "       <SUP_NAME>美格智能技术股份有限公司</SUP_NAME>" +
                "       <DISABLE>N</DISABLE>" +
                "       <ERP_SUP_ID>164</ERP_SUP_ID>" +
                "       <ERP_SUP_NO>材料采购</ERP_SUP_NO>" +
                "       <OU_ID>81</OU_ID>" +
                "      </H></head>" +
                "</root>";
//        RmGetDataApiService rmGetDataApiService = new RmGetDataApiService();
//        String docType = "CUX_PO_VENDOR_Q";
//        Map<String, String> map = new HashMap<String, String>();
//            map.put("STARTTIME","201804270002");
//            map.put("ENDTIME","201805270002");
//            map.put("ORG_ID","81");
//        String xmlStr = rmGetDataApiService.getDate(docType, map);
//            System.out.println("请求返回结果:"+xmlStr);
    
//        Document document = DocumentHelper.parseText(xmlStr);
//        Element root = document.getRootElement();
//        System.out.println("root:" + root.getName());
//        Element node=root.element("X_RESPONSE_DATA");
        //        System.out.println(node.getStringValue());
//        Document document1 = DocumentHelper.parseText(node.getStringValue());
//        Element node1 = root.element("head");
//        xmlStr = root.getStringValue();
        xmlStr = xmlStr.replace("<head>","");
        xmlStr = xmlStr.replace("</head>","");
        xmlStr = xmlStr.replace("","");
            try {
            JAXBContext context = JAXBContext.newInstance(Table.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Table table = (Table)unmarshaller.unmarshal(new StringReader(xmlStr));
            System.out.println(table.getRoot());
            if(table.getRoot() != null){
                for (int i = 0 ; i < table.getRoot().size() ; i++){
                    System.out.println(table.getRoot().get(i).toString());
                }
            }
        } catch (
        JAXBException e) {
            e.printStackTrace();
        }
    }
}
